// 前端分页管理器
class PaginationManager {
    constructor(containerId, options = {}) {
        this.containerId = containerId;
        this.data = [];
        this.filteredData = [];
        this.currentPage = 1;
        this.pageSize = options.pageSize || 20;
        this.totalPages = 1;
        
        // 元素选择器
        this.selectors = {
            total: options.totalSelector,
            pageSize: options.pageSizeSelector,
            currentPage: options.currentPageSelector,
            totalPages: options.totalPagesSelector,
            firstBtn: options.firstBtnSelector,
            prevBtn: options.prevBtnSelector,
            nextBtn: options.nextBtnSelector,
            lastBtn: options.lastBtnSelector
        };
        
        // 回调函数
        this.onPageChange = options.onPageChange || (() => {});
        this.onPageSizeChange = options.onPageSizeChange || (() => {});
        
        this.initEventListeners();
    }
    
    // 初始化事件监听器
    initEventListeners() {
        // 页面大小变化
        const pageSizeSelect = document.getElementById(this.selectors.pageSize);
        if (pageSizeSelect) {
            pageSizeSelect.addEventListener('change', (e) => {
                this.setPageSize(parseInt(e.target.value));
            });
        }
        
        // 分页按钮事件
        const firstBtn = document.getElementById(this.selectors.firstBtn);
        const prevBtn = document.getElementById(this.selectors.prevBtn);
        const nextBtn = document.getElementById(this.selectors.nextBtn);
        const lastBtn = document.getElementById(this.selectors.lastBtn);
        
        if (firstBtn) firstBtn.addEventListener('click', () => this.goToPage(1));
        if (prevBtn) prevBtn.addEventListener('click', () => this.goToPage(this.currentPage - 1));
        if (nextBtn) nextBtn.addEventListener('click', () => this.goToPage(this.currentPage + 1));
        if (lastBtn) lastBtn.addEventListener('click', () => this.goToPage(this.totalPages));
    }
    
    // 设置数据
    setData(data) {
        this.data = data || [];
        this.filteredData = [...this.data];
        this.updatePagination();
    }
    
    // 设置过滤后的数据
    setFilteredData(filteredData) {
        this.filteredData = filteredData || [];
        this.currentPage = 1; // 重置到第一页
        this.updatePagination();
    }
    
    // 获取当前页数据
    getCurrentPageData() {
        const startIndex = (this.currentPage - 1) * this.pageSize;
        const endIndex = startIndex + this.pageSize;
        return this.filteredData.slice(startIndex, endIndex);
    }
    
    // 跳转到指定页
    goToPage(page) {
        if (page < 1 || page > this.totalPages || page === this.currentPage) return;
        this.currentPage = page;
        this.updatePagination();
        this.onPageChange(page, this.getCurrentPageData());
    }
    
    // 设置页面大小
    setPageSize(size) {
        if (size <= 0 || size === this.pageSize) return;
        this.pageSize = size;
        this.currentPage = 1; // 重置到第一页
        this.updatePagination();
        this.onPageSizeChange(size, this.getCurrentPageData());
    }
    
    // 更新分页信息
    updatePagination() {
        const total = this.filteredData.length;
        this.totalPages = Math.max(1, Math.ceil(total / this.pageSize));
        
        // 确保当前页在有效范围内
        if (this.currentPage > this.totalPages) {
            this.currentPage = this.totalPages;
        }
        
        // 更新UI显示
        this.updateUI(total);
        this.updateButtons();
    }
    
    // 更新UI显示
    updateUI(total) {
        const totalElem = document.getElementById(this.selectors.total);
        const currentPageElem = document.getElementById(this.selectors.currentPage);
        const totalPagesElem = document.getElementById(this.selectors.totalPages);
        
        if (totalElem) totalElem.textContent = total;
        if (currentPageElem) currentPageElem.textContent = this.currentPage;
        if (totalPagesElem) totalPagesElem.textContent = this.totalPages;
    }
    
    // 更新按钮状态
    updateButtons() {
        const firstBtn = document.getElementById(this.selectors.firstBtn);
        const prevBtn = document.getElementById(this.selectors.prevBtn);
        const nextBtn = document.getElementById(this.selectors.nextBtn);
        const lastBtn = document.getElementById(this.selectors.lastBtn);
        
        const isFirstPage = this.currentPage === 1;
        const isLastPage = this.currentPage === this.totalPages;
        
        if (firstBtn) firstBtn.disabled = isFirstPage;
        if (prevBtn) prevBtn.disabled = isFirstPage;
        if (nextBtn) nextBtn.disabled = isLastPage;
        if (lastBtn) lastBtn.disabled = isLastPage;
    }
    
    // 获取分页信息
    getPageInfo() {
        return {
            currentPage: this.currentPage,
            pageSize: this.pageSize,
            totalPages: this.totalPages,
            total: this.filteredData.length,
            hasNext: this.currentPage < this.totalPages,
            hasPrev: this.currentPage > 1
        };
    }
}

// 创建全局分页管理器实例
window.connectionsPagination = new PaginationManager('connections-pagination', {
    pageSize: 20,
    totalSelector: 'connections-total',
    pageSizeSelector: 'connections-page-size',
    currentPageSelector: 'connections-current-page',
    totalPagesSelector: 'connections-total-pages',
    firstBtnSelector: 'connections-first-page',
    prevBtnSelector: 'connections-prev-page',
    nextBtnSelector: 'connections-next-page',
    lastBtnSelector: 'connections-last-page',
    onPageChange: (page, data) => {
        console.log('连接数据页面变更:', page, data.length);
        if (window.processRenderer) {
            window.processRenderer.renderConnections(data);
        }
    },
    onPageSizeChange: (size, data) => {
        console.log('连接数据页面大小变更:', size, data.length);
        if (window.processRenderer) {
            window.processRenderer.renderConnections(data);
        }
    }
});

window.processesPagination = new PaginationManager('processes-pagination', {
    pageSize: 20,
    totalSelector: 'processes-total',
    pageSizeSelector: 'processes-page-size',
    currentPageSelector: 'processes-current-page',
    totalPagesSelector: 'processes-total-pages',
    firstBtnSelector: 'processes-first-page',
    prevBtnSelector: 'processes-prev-page',
    nextBtnSelector: 'processes-next-page',
    lastBtnSelector: 'processes-last-page',
    onPageChange: (page, data) => {
        console.log('进程数据页面变更:', page, data.length);
        if (window.processRenderer) {
            window.processRenderer.renderProcesses(data);
        }
    },
    onPageSizeChange: (size, data) => {
        console.log('进程数据页面大小变更:', size, data.length);
        if (window.processRenderer) {
            window.processRenderer.renderProcesses(data);
        }
    }
});